package st.business.tepth.business.visitor.visitorwork.work.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.ArrayList;

import butterknife.BindView;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.visitor.visitorwork.entity.WorkDetailBean;
import st.shadow.library.core.base.fragment.BaseFragment;
import st.shadow.library.core.util.utilcode.util.ScreenUtils;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/6/5
 */
public class MaintainDelegate extends BaseFragment {
    public static MaintainDelegate newInstance(ArrayList<WorkDetailBean.MaintainBean> list) {
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("list", list);
        MaintainDelegate delegate = new MaintainDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private MaintainAdapter mMaintainAdapter;
    private ArrayList<WorkDetailBean.MaintainBean> mMaintainBeans;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mMaintainBeans = bundle.getParcelableArrayList("list");
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_work_read_only_detail_maintain;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mMaintainAdapter = new MaintainAdapter(new DataConvert()
                .convertMaintain(mMaintainBeans));
        mRecyclerView.setAdapter(mMaintainAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MultipleItemEntity entity = (MultipleItemEntity) baseQuickAdapter.getItem(i);
                if (entity != null) {
                    DialogMaintainFinishStandard dialogMaintainFinishStandard =
                            new DialogMaintainFinishStandard(_mActivity,
                                    (String) entity.getField("standard"));
                    Window dialogWindow = dialogMaintainFinishStandard.getWindow();
                    if (dialogWindow != null) {
                        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                        lp.width = ScreenUtils.getScreenWidth() * 1000 / 1280; // 宽度
                        lp.height = ScreenUtils.getScreenHeight() * 500 / 1024; // 高度
                        dialogWindow.setGravity(Gravity.CENTER);
                        dialogWindow.setAttributes(lp);
                        dialogMaintainFinishStandard.show();
                    }
                }
            }
        });
        mMaintainAdapter.setEmptyView(R.layout.view_search_empty,
                (ViewGroup) mRecyclerView.getParent());
    }

    public void resetData(ArrayList<WorkDetailBean.MaintainBean> maintainBeans) {
        if (mMaintainAdapter != null) {
            mMaintainAdapter.setNewData(new DataConvert()
                    .convertMaintain(maintainBeans));
        }
    }

}
